<template>
  <section>
    <main-bar></main-bar>
    <h3>print</h3>
    <p>请输入您要打印的信息</p>
    <el-input v-model="teststr" placeholder="输入您要测试的文字"></el-input>
    <div>
      <el-button @click.native="printSystem">system打印</el-button>
      <el-button @click.native="printStill">静默打印</el-button>
      <el-button @click.native="printPreview">预览</el-button>
    </div>
    <div>
      <!-- <img ref="base64" src="@/assets/img/base.png" alt="png"> -->
    </div>
  </section>
</template>

<script>
const ipcRenderer = require('electron').ipcRenderer
import pring from '@/components/pring'
const path = require('path')
const execFile = require('child_process').execFile
export default {
  name: 'print',
  data () {
    return {
      teststr: '',
      xmlHttp: null,
      printList: null,
      reader: new FileReader(),
      base64: '13545'
    }
  },
  components: {
    // 'pring': pring
  },
  mounted () {
    ipcRenderer.send('getPrinterList');
    this.init()
  },
  methods: {
    init () {
      // 监听主线程获取到打印机列表后的回调
      ipcRenderer.once('getPrinterList', (event, data) => {
        console.log('打印机列表', data);
        this.printList = data
      })
      ipcRenderer.once('print-view-source', (event, data) => {
        console.log('预览资源', data);
        // 1.赋值给img 2.并且展示出来，3.并且显示打印按钮。
      })
    },
    printSystem () {
      let formatJson = '{"items":[{"type":"row"},{"text":"某某店","type":"text","fontSize":"12","stringAlignment":"Center"},{"type":"row"},{"type":"row"},{"text":"购物时间：2018-09-14 11:00:28","fontSize":"8","type":"text"},{"type":"row"},{"type":"table","model":"2","columns":[{"type":"text","text":"名称","width":"0.6","name":"Name"},{"type":"text","text":"数量","width":"0.2","offset":"0.6","name":"Quantity"},{"type":"text","text":"小计","width":"0.2","offset":"0.8","name":"PriceTotal"}],"rows":[[{"type":"text","text":"乐事薯片美国经典-原味-40克1 ","width":"0.6"},{"type":"text","text":1,"offset":"0.6","width":"0.2"},{"type":"text","text":"0.02","offset":"0.8","width":"0.2"}],[{"type":"text","text":"乐事薯片无限忠于(原味)桶装104克 ","width":"0.6"},{"type":"text","text":2,"offset":"0.6","width":"0.2"},{"type":"text","text":"20.00","offset":"0.8","width":"0.2"}]]},{"type":"row"},{"text":"总数:3","type":"text"},{"text":"应收:20.02","type":"text","offset":"0.6","width":"0.4"},{"type":"row"},{"type":"row"},{"text":"商品免费送到家，更享多重优惠！","type":"text","stringAlignment":"Center"},{"type":"row"}],"width":"58"}'
      let pathName = path.resolve(__dirname, '../../utils')
      let pint = execFile(path.join(pathName, '/cashAdpter/CashAdpter.Main.exe'), 
      // ['print', JSON.stringify(formatJson)], 需要先用方法转换打印数据结构后(一定要标注的json格式)，再测试是否需要JSON.stringify
          ['print', formatJson], 
          (error, stdout, stderr) => {
          if (error) {
            throw error;
          }
          console.log('system打印', stdout, stderr)
      })
    },
    printStill () {
      let pintSource = {
        imgSource: this.teststr,
        imgWidth: '768' + 'px',
        imgHeight: '1028' + 'px'
      }
      ipcRenderer.send('main-do-print', pintSource);
    },
    printPreview () {
      ipcRenderer.send('main-review-print');
    }
  }
}
</script>
